<?php

namespace Dongasai\WkProtoPhp\Enums;

/**
 * 频道类型枚举
 * 使用PHP 8枚举语法
 */
enum ChannelType: int
{
    case PERSON = 1;           // 个人频道
    case GROUP = 2;             // 群组频道
    case CUSTOMER_SERVICE = 3;  // 客服频道（建议新实现使用访客频道代替）
    case COMMUNITY = 4;        // 社区频道
    case COMMUNITY_TOPIC = 5;  // 社区话题频道
    case INFO = 6;             // 信息频道（具有临时订阅者概念）
    case DATA = 7;             // 数据频道
    case TEMP = 8;             // 临时频道
    case LIVE = 9;             // 直播频道（不保存最近会话数据）
    case VISITORS = 10;        // 访客频道（在新实现中替代客服频道）
    case AGENT = 11;           // AI Agent单聊频道
    case AGENT_GROUP = 12;     // AI Agent群聊频道
    

    /**
     * 获取频道类型的字符串表示
     */
    public function toString(): string
    {
        return $this->name;
    }

    /**
     * 从值创建枚举实例
     */
    public static function fromValue(int $value): self
    {
        return self::from($value);
    }

    /**
     * 向后兼容的toString方法
     */
    public static function toStringFromValue(int $type): string
    {
        try {
            return self::from($type)->name;
        } catch (\ValueError $e) {
            return sprintf('UNKNOWN[%d]', $type);
        }
    }

    /**
     * 检查是否为个人频道
     */
    public function isPersonal(): bool
    {
        return $this === self::PERSON;
    }

    /**
     * 检查是否为群组频道
     */
    public function isGroup(): bool
    {
        return $this === self::GROUP;
    }

    /**
     * 检查是否为临时频道
     */
    public function isTemporary(): bool
    {
        return in_array($this, [self::TEMP, self::VISITORS, self::INFO]);
    }

    /**
     * 检查是否为AI Agent相关频道
     */
    public function isAgentType(): bool
    {
        return in_array($this, [self::AGENT, self::AGENT_GROUP]);
    }

    /**
     * 检查是否为社区相关频道
     */
    public function isCommunityType(): bool
    {
        return in_array($this, [self::COMMUNITY, self::COMMUNITY_TOPIC]);
    }

    /**
     * 检查是否为数据频道
     */
    public function isDataType(): bool
    {
        return $this === self::DATA;
    }

    /**
     * 检查是否为直播频道
     */
    public function isLiveType(): bool
    {
        return $this === self::LIVE;
    }

    /**
     * 检查是否为客服相关频道
     */
    public function isCustomerServiceType(): bool
    {
        return in_array($this, [self::CUSTOMER_SERVICE, self::VISITORS]);
    }

    /**
     * 检查是否为信息频道
     */
    public function isInfoType(): bool
    {
        return $this === self::INFO;
    }

    /**
     * 检查是否支持持久化会话
     */
    public function supportsPersistentSession(): bool
    {
        return !in_array($this, [self::LIVE, self::TEMP, self::INFO]);
    }

    /**
     * 获取频道类型描述
     */
    public function getDescription(): string
    {
        return match ($this) {
            self::PERSON => '个人频道',
            self::GROUP => '群组频道',
            self::CUSTOMER_SERVICE => '客服频道',
            self::COMMUNITY => '社区频道',
            self::COMMUNITY_TOPIC => '社区话题频道',
            self::INFO => '资讯频道',
            self::DATA => '数据频道',
            self::TEMP => '临时频道',
            self::LIVE => '直播频道',
            self::VISITORS => '访客频道',
            self::AGENT => 'AI Agent单聊频道',
            self::AGENT_GROUP => 'AI Agent群聊频道',
        };
    }

    /**
     * 获取频道类型的特征
     */
    public function getFeatures(): array
    {
        return match ($this) {
            self::PERSON => ['one_to_one', 'persistent', 'encryption'],
            self::GROUP => ['multi_user', 'persistent', 'encryption', 'history'],
            self::CUSTOMER_SERVICE => ['one_to_many', 'persistent', 'queue'],
            self::COMMUNITY => ['broadcast', 'persistent', 'history'],
            self::COMMUNITY_TOPIC => ['topic_based', 'persistent', 'subscription'],
            self::INFO => ['broadcast', 'temporary_subscription', 'push_notification'],
            self::DATA => ['structured', 'binary', 'streaming'],
            self::TEMP => ['ephemeral', 'auto_cleanup', 'no_history'],
            self::LIVE => ['streaming', 'real_time', 'no_history'],
            self::VISITORS => ['guest_mode', 'temporary', 'multi_agent'],
            self::AGENT => ['ai_assisted', 'intelligent', 'context_aware'],
            self::AGENT_GROUP => ['multi_agent', 'collaborative', 'ai_enhanced'],
        };
    }

    /**
     * 获取推荐使用的频道类型（对于客服频道，推荐使用访客频道）
     */
    public function getRecommendedType(): self
    {
        return match ($this) {
            self::CUSTOMER_SERVICE => self::VISITORS,
            default => $this,
        };
    }

    /**
     * 检查是否为推荐使用的频道类型
     */
    public function isRecommended(): bool
    {
        return $this !== self::CUSTOMER_SERVICE;
    }
}